Repository navigation
fix(sqlite): Expand multi-column assignments into individual assignments - #4709
Open
manuelschoene wants to merge 2 commits into
Open
manuelschoene wants to merge 2 commits into
manuelschoene wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In SQLite,
SET (a, b) = (?, ?)(inUPDATEand in an upsert'sDO UPDATE SET) names and types every parameter in the row value after the first column of the assignment. Two columns of different types therefore produced wrong Go types (e.g.,Name_2 stringfor anINTEGERcolumn). On the core analyzer path, the parameters came out asColumn1 any/Column2 any.The cause was
convertSetPairsininternal/engine/sqlite/convert.go: it converted the right-hand side once and gave the whole row value to every column on the left.Fixes #4708.
Changes
convertSetPairsnow gives each column the element of the row value at its position, when the right-hand side is a row value with exactly as many elements as there are columns.(name, score) = (?, ?)becomesname = ?, score = ?in sqlc's AST, so parameter discovery and the core analyzer see ordinary single-column assignments and need no changes.rowElements, which also looks through redundant parentheses (((?, ?))).(a, b) = (SELECT ...), keeps the previous behavior.I did not reuse
ast.MultiAssignRef(as the PostgreSQL engine does) for two reasons: the core analyzer does not handle it, andUpdateStmt.Formatmerges every target into a single tuple once one of them is aMultiAssignRef, which would printSET (a, b) = (?, ?), c = ?incorrectly.Tests
New end-to-end case
internal/endtoend/testdata/update_set_multiple_columns/sqlite. The columns have different types (TEXT/INTEGER), which the existingupdate_set_multiplecases do not cover:UPDATE t SET (name, score) = (?, ?) WHERE id = ?SET (score, name) = (?, ?)SET (name, score) = (?, ?), created = ?INSERT ... ON CONFLICT (id) DO UPDATE SET (name, score) = (?, ?)The golden file was written before the fix. It is the output of the equivalent single-column assignments (
SET name = ?, score = ?), with only the SQL text converted back to tuple form. It failed before the fix and passes after it, with no changes. All four queries are accepted bysqlite33.54.1.TestReplay,TestFormat, andTestJsonSchemapass. Database-backed tests were not run locally.SQLC_TEST_CORE=1): the threeUPDATEqueries now generate correctly. The case still fails in that context, because the core analyzer ignoresON CONFLICT DO UPDATEand drops the upsert'sDO UPDATEparameters entirely. That is a separate issue. The set of failing core cases is identical before and after this change.CI
I whitelisted four logic errors from the managed-db context. The CI tests the queries of the end-to-end tests against a live SQLite database instance. This instance is not built with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT option enabled. Therefore, the syntax is unknown. As this is expected behavior, I mirrored the whitelist design used in other test cases, such as
invalid_params.Not in this PR
While testing the subquery fallback, I found that
UPDATE t SET (name, score) = (SELECT name, score FROM t WHERE id = ?) WHERE id = ?fails on the default path withcolumn reference "id" is ambiguous. It is unrelated to this change: it also happens without it, for single-column assignments, and for plainSELECT ... WHERE x < (SELECT ... FROM t WHERE id = ?)on SQLite and PostgreSQL. It is already reported as #4251 and #3234.